package com.handpay.zztong.hp;


import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.HttpEngine;
import com.handpay.framework.LuaTableUtil;
import com.handpay.framework.ui.PullToRefreshView;
import com.handpay.framework.ui.PullToRefreshView.OnFooterRefreshListener;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.framework.utils.DateUtils;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.PurchaseStatus;
import com.handpay.zztong.hp.config.ZZTConfig;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.protocol.ATProtocolConstant;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import se.krka.kahlua.vm.LuaTable;

public class GfFoundTransactionRecords extends ZZTongActivity implements OnFooterRefreshListener,OnItemSelectedListener,OnItemClickListener{
	private Spinner guangfa_record_type_spinner;
	private PullToRefreshView mPullToRefreshView; 
	private ListView guangfa_record_listview;
	private MyAdapter myadapter;
	public static int record_type = 0;//近一个月的历史记录类型 0：收益 。 1：申购。 2：赎回
	private int pageNo =1;
	private final  int pageSize = 10;
	public static double totalCnt ;//对应类型的总记录条数，用来判断是否要再次请求
	private List<LuaTable> tlist = new ArrayList<LuaTable>();
	   @Override
	protected void onCreate(Bundle savedInstanceState) {
	    this.setContentView(R.layout.gffound_transaction_record);
		super.onCreate(savedInstanceState);
		init();
		doPost(this,Do_ZztGFReturnHistory , treatRecordsParams(), true);
	}
	//初始化控件
	public void init(){
//		((TextView) findViewById(R.id.title))
//		.setText(getString(R.string.guangfa_transaction_records));
//		myActionBar.setTitleText(R.string.guangfa_transaction_records);// title
		guangfa_record_type_spinner = (Spinner) findViewById(R.id.guangfa_record_type_spinner);
		mPullToRefreshView = (PullToRefreshView)findViewById(R.id.main_pull_refresh_view);
		guangfa_record_listview = (ListView) findViewById(R.id.guangfa_record_listview);
		myadapter = new MyAdapter();
		 ArrayAdapter<CharSequence> record_type_adapter = ArrayAdapter.createFromResource(
					this, R.array.guangfa_record_type, android.R.layout.simple_spinner_item);
		 record_type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		 guangfa_record_type_spinner.setAdapter(record_type_adapter);
		 guangfa_record_type_spinner.setOnItemSelectedListener(this);
		 guangfa_record_listview.setOnItemClickListener(this);
	     mPullToRefreshView.setOnFooterRefreshListener(this);
	     mPullToRefreshView.mFooterView.setVisibility(View.GONE);
	}
	   @Override
	protected void onResume() {
		super.onResume();
	}
	   @Override
	protected boolean isShownTabs() {
		return false;
	}
	   @Override
	protected boolean isShownTopBar() {
		return true;
	}
	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		HPLog.e("fyang", "spinner"+position);
		record_type = position;
        pageNo = 1;//一旦更换了记录类型就把pageNo还原到1
		if(tlist !=null ){
			tlist.clear();
		}
		if(position == 0){
			doPost(this, Do_ZztGFReturnHistory, treatRecordsParams(), true);
		}else if(position ==1){
			doPost(this, Do_ZztGFPurchaseHistory, treatRecordsParams(), true);
		}else if(position ==2){
			doPost(this, Do_ZztGFPurchaseHistory, treatRecordsParams(), true);
		}
	}
	@Override
	public void onNothingSelected(AdapterView<?> parent) {

	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		 HPLog.e("fyang", "listview"+position);
		 //只有申购和赎回才有详情
		 if(record_type != 0){
			 Hashtable<String, String> params = new Hashtable<String, String>();// 请求历史详情的参数
			 params.put(BaseProtocolKey.CHANNEL, ClientEngine.CHANNEL);
			 params.put(ATProtocolConstant.CLIENTCHANNEL, ZZTConfig.UPDATE_CHANNEL);
			 params.put(BaseProtocolKey.PRODUCTCODE, "HLC");
			 params.put("recordId", (String)tlist.get(position).rawget("recordId"));
			 HPLog.e("fyang", "申购或者赎回详情查询走起"+ tlist.get(position).rawget("recordId"));
			 doPost(this, Do_ZztPurchaseRecordDetail, params,true); 
		 }

	}
	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		//如果总条数多余10条，那就需要分页加载，再次去请求
		if(totalCnt >tlist.size() && record_type !=0){
			mPullToRefreshView.mFooterView.setVisibility(View.VISIBLE);
			pageNo++;
			doPost(this,Do_ZztGFPurchaseHistory , treatRecordsParams(), false);
		}else {
		  mPullToRefreshView.postDelayed(new Runnable() {
				@Override
				public void run() {
					mPullToRefreshView.onFooterRefreshComplete();
				}
			}, 1000);
		}
	}
	@Override
	public boolean netResponse(String action, Hashtable<String, Object> resp,
			boolean showAlert, OnClickListener on) {
		boolean result = super.netResponse(action, resp, showAlert, on);
		HPLog.e("fyang", "super netresponse result"+result);
		closeProgressDialog();
		if(result == true){
			 mPullToRefreshView.mFooterView.setVisibility(View.GONE);
			 guangfa_record_listview.setAdapter(myadapter);
		     this.setResult(Activity.RESULT_CANCELED);
		     return true;
	    }
		 try {
			 byte data[] = (byte[]) resp.get(HttpEngine.Key_RespData);
			 LuaTable luaTable = LuaTableUtil.stringToLuaTable(data, isDataCompile);
			 HPLog.e("fyang", "打印后台返回数据开始");
			 LuaTableUtil.printTable(luaTable);
			 HPLog.e("fyang", "打印后台返回数据结束");
			if(action.equals(Do_ZztGFReturnHistory)){
				//收益的回调
				mPullToRefreshView.mFooterView.setVisibility(View.GONE);
				tlist =getRecords(luaTable);
				guangfa_record_listview.setAdapter(myadapter);
				guangfa_record_listview.setSelection(ListView.FOCUS_DOWN);//每次加载之后,都滚动到最下方
			}else if(action.equals(Do_ZztGFPurchaseHistory)){
				//理财的回调包括申购和赎回
				tlist =getRecords(luaTable);
				if(tlist!=null &&tlist.size()>10){
					mPullToRefreshView.mFooterView.setVisibility(View.VISIBLE);
				}else {
					mPullToRefreshView.mFooterView.setVisibility(View.GONE);
				}
				totalCnt = (Double) luaTable.rawget("totalCnt");
				if(pageNo>1){
			       mPullToRefreshView.postDelayed(new Runnable() {
					@Override
					public void run() {
						mPullToRefreshView.onFooterRefreshComplete();
					}
				}, 1000);
				}
				guangfa_record_listview.setAdapter(myadapter);
				guangfa_record_listview.setSelection(ListView.FOCUS_DOWN);//每次加载之后,都滚动到最下方
			}else if(action.equals(Do_ZztPurchaseRecordDetail)){
				//详情的回调,只有申购详情和赎回详情
				HPLog.e("fyang", "详情的回调");
				LuaTable purchase = (LuaTable) luaTable.rawget("productDetail");
					if(purchase !=null){
			   	 	    PurchaseStatus status = PurchaseStatus.generatePurchaseStatus(purchase);	   	 	    
				   	 	    if(status != null){
				   	 	    	if("REDEMPTION".equals(status.getPurchaseType())){
				   	 	    			Intent it = new Intent(this,RedemptionResult.class);
				   	 	    			it.putExtra("purchaseStatus", status);
				   	 	    			startActivity(it);
				   	 	    	}else{
				   	 	    		Intent it = new Intent(this,ApplyPurchaseResult.class);
				   	 	    		it.putExtra("purchaseStatus", status);
				   	 	    		startActivity(it);
				   	 	    	}
				   	 	    }else{
				   	 	        dealError(null, "获取详情出错，请稍后重新尝试");
				   	 	    }
			   	 		}else{
				   	 			String responseCode = (String) luaTable.rawget("responseCode");
				   	 			String errorMessage = (String) luaTable.rawget("errMessage");
				   	 		    dealError(responseCode, errorMessage);
			   	 		}
			    }
		} catch (Exception e) {
			e.printStackTrace();
			dealError(null, getString(R.string.guangfa_records_error));
		}
		 return false;
	}
    /**
     * 组织历史记录查询的参数
     * 区别：收益的记录是一次性全部加载
     *     申购和赎回的是分页滑动加载
     * @return
     */
	public  Hashtable<String, String> treatRecordsParams(){
	     Hashtable<String, String> params = new Hashtable<String, String>();// 请求的参数
		 params.put(BaseProtocolKey.CHANNEL, ClientEngine.CHANNEL);
		 params.put(ATProtocolConstant.CLIENTCHANNEL, ZZTConfig.UPDATE_CHANNEL);
		 params.put(BaseProtocolKey.PRODUCTCODE, "HLC");
	      if(record_type ==1){
	    	 params.put("pageNo", pageNo+"");
	    	 params.put("pageSize", pageSize+"");
	    	 params.put("purchaseType", "PURCHASE");
	      }else if(record_type ==2){
	    	 params.put("pageNo", pageNo+"");
	    	 params.put("pageSize", pageSize+"");
	    	 params.put("purchaseType", "REDEMPTION");
		 }
		 return params;
     }
	class IncomeHolder{
		TextView text_income_returnDate;
		TextView text_income_amount;
	}
	class PurchaseHolder{
		TextView purchase_apply_date_text;
		TextView purchase_date_text;
		TextView purchase_money_text;
		TextView purchase_status_text;
	}
	class RansomHolder{
		TextView ransom_date_text;
		TextView ransom_money_text;
		TextView ransom_status_text;
	}
	/**
	 * 自定义适配器
	 * @author fyang
	 *
	 */
	public class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return tlist !=null?tlist.size():0;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			IncomeHolder incomeHolder;
			PurchaseHolder purchaseHolder;
			RansomHolder ransomHolder;
			String st  = null;
			String text= null;
		    int fstart;
            int fend;
            SpannableStringBuilder style =null;
			try {
				if (convertView == null) {
					convertView = View.inflate(getApplicationContext(),
							R.layout.gffound_records_item, null);
					RelativeLayout guangfa_income_item = (RelativeLayout) convertView.findViewById(R.id.guangfa_income_item);
					RelativeLayout guangfa_purchase_item = (RelativeLayout) convertView.findViewById(R.id.guangfa_purchase_item);
					RelativeLayout guangfa_ransom_item = (RelativeLayout) convertView.findViewById(R.id.guangfa_ransom_item);
					if(tlist == null || tlist.size() ==0){
						 guangfa_income_item.setVisibility(View.GONE);
				         guangfa_purchase_item.setVisibility(View.GONE);
				         guangfa_ransom_item.setVisibility(View.GONE);
					}
					if (record_type == 0) {
						// 收益
				        guangfa_income_item.setVisibility(View.VISIBLE);
				        guangfa_purchase_item.setVisibility(View.GONE);
				        guangfa_ransom_item.setVisibility(View.GONE);
						incomeHolder = new IncomeHolder();
						incomeHolder.text_income_returnDate = (TextView) convertView.findViewById(R.id.text_income_returnDate);
					    incomeHolder.text_income_amount = (TextView) convertView.findViewById(R.id.text_income_amount);
					    String time = "";
					    try{
					    	time =(String)tlist.get(position).rawget("returnDate").toString().subSequence(0, 8);
					    }catch(Exception e){
					    	e.printStackTrace();
					    }
					    incomeHolder.text_income_returnDate.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(time, "yyyyMMdd"), "yyyy-MM-dd"));
						if( Double.parseDouble(tlist.get(position).rawget("amount").toString())/100 >= 0){
							incomeHolder.text_income_amount.setText("+"+ CommonUtils.formatMoneyDouble(Double.parseDouble(tlist.get(position).rawget("amount").toString())/100));
							incomeHolder.text_income_amount.setTextColor(Color.parseColor("#24CC3A"));
						}else {
							
							incomeHolder.text_income_amount.setText(CommonUtils.formatMoneyDouble(Double.parseDouble(tlist.get(position).rawget("amount").toString())/100));
							incomeHolder.text_income_amount.setTextColor(Color.RED);
						} 
						convertView.setTag(incomeHolder);
					} else if (record_type == 1) {
						// 申购
				        guangfa_income_item.setVisibility(View.GONE);
				        guangfa_purchase_item.setVisibility(View.VISIBLE);
				        guangfa_ransom_item.setVisibility(View.GONE);
				        purchaseHolder = new PurchaseHolder();
				        purchaseHolder.purchase_apply_date_text = (TextView) convertView.findViewById(R.id.purchase_apply_date_text);
				        purchaseHolder.purchase_date_text = (TextView) convertView.findViewById(R.id.purchase_date_text);
				        purchaseHolder.purchase_money_text = (TextView) convertView.findViewById(R.id.purchase_money_text);
				        purchaseHolder.purchase_status_text = (TextView) convertView.findViewById(R.id.purchase_status_text); 
				        String status = (String)tlist.get(position).rawget("status");//先拿到当前记录的状态
				        purchaseHolder.purchase_apply_date_text.setText("申请时间:"+DateUtils.formatDateToString(DateUtils.formatStringToDate((String)tlist.get(position).rawget("requestDate"), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
				        if("WW".equals(status) || "W1".equals(status)/* || "01".equals(status)*/){
				        	purchaseHolder.purchase_date_text.setVisibility(View.GONE);
				        }else{
				        	if(tlist.get(position).rawget("operationDate") != null){
				        		purchaseHolder.purchase_date_text.setVisibility(View.VISIBLE);
					        	purchaseHolder.purchase_date_text.setText("申购时间:"+DateUtils.formatDateToString(DateUtils.formatStringToDate((String)tlist.get(position).rawget("operationDate"), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
				        	}else{
				        		purchaseHolder.purchase_date_text.setVisibility(View.GONE);
				        	}
				        }
				        //给金额改变颜色
				        st = CommonUtils.formatMoneyDouble(Double.parseDouble(tlist.get(position).rawget("amount").toString())/100)+"元";
				        text = "申购金额:"+st;
				        fstart=text.indexOf(st);
				        fend=fstart+st.length();
				        style=new SpannableStringBuilder(text); 
				        style.setSpan(new ForegroundColorSpan(Color.GREEN),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        purchaseHolder.purchase_money_text.setText(style);
				        //给状态改变颜色
				         st = getStatusText(status);
				         text = "申购状态:"+st;
				         fstart=text.indexOf(st);
				         fend=fstart+st.length();
				         style=new SpannableStringBuilder(text);
				        if(st.contains("失败")){
				            style.setSpan(new ForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }else if(st.contains("成功"))
				        {
				        	style.setSpan(new ForegroundColorSpan(Color.GREEN),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }else if(st.contains("处理中")){
				        	style.setSpan(new ForegroundColorSpan(Color.BLUE),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }
				        purchaseHolder.purchase_status_text.setText(style);
				        convertView.setTag(purchaseHolder);
					} else if (record_type == 2) {
						// 赎回
				        guangfa_income_item.setVisibility(View.GONE);
				        guangfa_purchase_item.setVisibility(View.GONE);
				        guangfa_ransom_item.setVisibility(View.VISIBLE);
				        ransomHolder = new RansomHolder();
				        ransomHolder.ransom_date_text = (TextView) convertView.findViewById(R.id.ransom_date_text);
				        ransomHolder.ransom_money_text = (TextView) convertView.findViewById(R.id.ransom_money_text);
				        ransomHolder.ransom_status_text = (TextView) convertView.findViewById(R.id.ransom_status_text);
				        ransomHolder.ransom_date_text.setText("赎回时间:"+DateUtils.formatDateToString(DateUtils.formatStringToDate((String)tlist.get(position).rawget("operationDate"), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
				        //给金额改变颜色
				        st = CommonUtils.formatMoneyDouble(Double.parseDouble(tlist.get(position).rawget("amount").toString())/100)+"元";
				        text = "赎回金额:"+st;
				        fstart=text.indexOf(st);
				        fend=fstart+st.length();
				        style=new SpannableStringBuilder(text); 
				        style.setSpan(new ForegroundColorSpan(Color.GREEN),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        ransomHolder.ransom_money_text.setText(style);
				        //给状态改变颜色
				        st = getStatusText((String)tlist.get(position).rawget("status"));
					    text = "赎回状态:"+st;
					    fstart=text.indexOf(st);
				        fend=fstart+st.length();
				        style=new SpannableStringBuilder(text); 
				        if(st.contains("失败")){
				        	style.setSpan(new ForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }else if(st.contains("成功")){
				        	style.setSpan(new ForegroundColorSpan(Color.GREEN),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }else if(st.contains("处理中")){
				        	style.setSpan(new ForegroundColorSpan(Color.BLUE),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }
				        ransomHolder.ransom_status_text.setText(style);
				        convertView.setTag(ransomHolder);
					}
				} else {
					HPLog.e("fyang", "convertView is not  null");
					if (record_type == 0) {
						incomeHolder = (IncomeHolder) convertView.getTag();
						incomeHolder.text_income_returnDate.setText(DateUtils.formatDateToString(DateUtils.formatStringToDate(tlist.get(position).rawget("returnDate").toString().substring(0, 8), "yyyyMMdd"), "yyyy-MM-dd"));
						if( Double.parseDouble(tlist.get(position).rawget("amount").toString())/100 >= 0){
							incomeHolder.text_income_amount.setText("+"+ CommonUtils.formatMoneyDouble(Double.parseDouble(tlist.get(position).rawget("amount").toString())/100));
							incomeHolder.text_income_amount.setTextColor(Color.parseColor("#24CC3A"));
						}else {
							
							incomeHolder.text_income_amount.setText(CommonUtils.formatMoneyDouble(Double.parseDouble(tlist.get(position).rawget("amount").toString())/100));
							incomeHolder.text_income_amount.setTextColor(Color.RED);
						} 
					} else if (record_type == 1) {
				        purchaseHolder = (PurchaseHolder) convertView.getTag();
				        String status = (String)tlist.get(position).rawget("status");//先拿到当前记录的状态
				        purchaseHolder.purchase_apply_date_text.setText("申请时间:"+DateUtils.formatDateToString(DateUtils.formatStringToDate((String)tlist.get(position).rawget("requestDate"), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
				        if("WW".equals(status) || "W1".equals(status)/*|| "01".equals(status)*/){
				        	purchaseHolder.purchase_date_text.setVisibility(View.GONE);
				        }else{
				        	if(tlist.get(position).rawget("operationDate") != null){
				        		purchaseHolder.purchase_date_text.setVisibility(View.VISIBLE);
					        	purchaseHolder.purchase_date_text.setText("申购时间:"+DateUtils.formatDateToString(DateUtils.formatStringToDate((String)tlist.get(position).rawget("operationDate"), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
				        	}else{
				        		purchaseHolder.purchase_date_text.setVisibility(View.GONE);
				        	}
				        }
				        //给金额改变颜色
				        st = CommonUtils.formatMoneyDouble(Double.parseDouble(tlist.get(position).rawget("amount").toString())/100)+"元";
				        text = "申购金额:"+st;
				        fstart=text.indexOf(st);
				        fend=fstart+st.length();
				        style=new SpannableStringBuilder(text); 
				        style.setSpan(new ForegroundColorSpan(Color.GREEN),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        purchaseHolder.purchase_money_text.setText(style);
				        //给状态改变颜色
				        st = getStatusText(status);
				        text = "申购状态:"+st;
				        fstart=text.indexOf(st);
				        fend=fstart+st.length();
				        style=new SpannableStringBuilder(text); 
				        if(st.contains("失败")){
				            style.setSpan(new ForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }else if(st.contains("成功"))
				        {
				        	style.setSpan(new ForegroundColorSpan(Color.GREEN),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }else if(st.contains("处理中")){
				        	style.setSpan(new ForegroundColorSpan(Color.BLUE),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }
				        purchaseHolder.purchase_status_text.setText(style);
					} else if (record_type == 2) {
				        ransomHolder = (RansomHolder) convertView.getTag();
				        ransomHolder.ransom_date_text.setText("赎回时间:"+DateUtils.formatDateToString(DateUtils.formatStringToDate((String)tlist.get(position).rawget("operationDate"), "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
				        //给金额改变颜色
				        st = CommonUtils.formatMoneyDouble(Double.parseDouble(tlist.get(position).rawget("amount").toString())/100)+"元";
				        text = "赎回金额:"+st;
				        fstart=text.indexOf(st);
				        fend=fstart+st.length();
				        style=new SpannableStringBuilder(text); 
				        style.setSpan(new ForegroundColorSpan(Color.GREEN),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        ransomHolder.ransom_money_text.setText(style);
				        //给状态改变颜色
				        st = getStatusText((String)tlist.get(position).rawget("status"));
					    text = "赎回状态:"+st;
					    fstart=text.indexOf(st);
				        fend=fstart+st.length();
				        style = new SpannableStringBuilder(text);  
				        if(st.contains("失败")){
				        	style.setSpan(new ForegroundColorSpan(Color.RED),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }else if(st.contains("成功")){
				        	style.setSpan(new ForegroundColorSpan(Color.GREEN),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }else if(st.contains("处理中")){
				        	style.setSpan(new ForegroundColorSpan(Color.BLUE),fstart,fend,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
				        }
				        ransomHolder.ransom_status_text.setText(style);
					}
				}
			} catch (NumberFormatException e) {
				e.printStackTrace();
				dealError(null, "系统错误,请稍后重新尝试");
			}
			return convertView;
		}
	}
	public List<LuaTable> getRecords(LuaTable table){
		try {
			Vector<LuaTable> items = null;
			if(record_type ==0){
				if(tlist!=null){tlist.clear();}
				items = LuaTableUtil.getArray((LuaTable)table.rawget("recordList"));
			}else if(record_type ==1 || record_type == 2){
				items = LuaTableUtil.getArray((LuaTable)table.rawget("purchaseHistoryRecords"));
			}
			if(items == null || items.size() == 0){
				HPLog.e("fyang", "items is null");
				String responseCode = (String) table.rawget("responseCode");
   	 			String errorMessage = (String) table.rawget("errMessage");
				dealError(responseCode, errorMessage);
			}
			for(int i=0;i<items.size();i++){
				tlist.add(items.get(i));
			}
			return tlist;
		} catch (Exception e) {
			e.printStackTrace();
			HPLog.e("fyang", "getRecords fail");
			dealError(null, getString(R.string.guangfa_records_error));
			return null;
		}
	}
	//  状态：WW申请成功W1申请失败 01处理中02失败00成功
	public String getStatusText(String code){
		String text = null;
		if("WW".equals(code)){
			text = "申请成功";
		}else if("W1".equals(code)){
			text ="申请失败";
		}else if("01".equals(code)){
			text ="处理中";
		}else if("02".equals(code)){
			if(record_type == 1){
				text = "申购失败";
			}else if(record_type == 2){
				text = "赎回失败";
			}
		}else if("00".equals(code)){
			if(record_type == 1){
				text = "申购成功";
			}else if(record_type == 2){
				text = "赎回成功";
			}
		}else {
			text ="未知状态";
		}
		return text;
	}
	private void dealError(String errorCode,String errorMessge){
		mPullToRefreshView.mFooterView.setVisibility(View.GONE);//一旦出错,就隐藏foot
		guangfa_record_listview.setAdapter(myadapter);
		tlist = new ArrayList<LuaTable>();//重置list
		showAlertDialog(this, getResources().getString(R.string.tip_title), errorMessge!=null?errorMessge:getString(R.string.guangfa_system_error), false, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
	}
    @Override
    public ActionBar getActivityActionBar() {
    	return new ActionBar(this.getString(R.string.guangfa_transaction_records),true);
    }
}
